/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 *   All Rights Reserved. See end of file
 */
package com.bmackay.maven.publisher;

import com.bmackay.maven.publisher.conf.ResourceBundleReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

/**
 * @author bmackay
 */
public class TimeStamps {

    private static File f = null;
    private static File fileIgnore = null;
    private static File fileIgnoreRoot = null;
    public static Hashtable ht = new Hashtable();
    public static Hashtable htIgnore = new Hashtable();
    public static Hashtable htIgnoreRoot = new Hashtable();

    public static void init() {
        getIgnore();
        getIgnoreRoot();
        getTimestamps(fileIgnore, htIgnore);
        getTimestamps(fileIgnoreRoot, htIgnoreRoot);
        get();
        getTimestamps(f, ht);
    }

    public static void get() {
        f = new File(Constants.timeStamps);
        if (f == null) {
            System.out.print("Error: check config file for proper path");
        }
    }

    public static void getIgnore() {
        fileIgnore = new File(Constants.ignoreFile);
        if (fileIgnore == null) {
            System.out.print("Error: check ignore file for proper path");
        }
    }

    public static void getIgnoreRoot() {
        fileIgnoreRoot = new File(Constants.ignoreRootFile);
        if (fileIgnoreRoot == null) {
            System.out.print("Error: check ignore root file for proper path");
        }
    }

    public static boolean check(File currFile) {

        boolean has = htIgnore.containsKey(currFile.getAbsolutePath());

        if(has == true){
            return true;
        }

        Enumeration e = htIgnoreRoot.keys();

        while (e.hasMoreElements()) {

            String root = (String) e.nextElement();
            if (root != null) {
                if (!root.equals("")) {
                    if (currFile.getAbsolutePath().contains(root)) {
                        return true;
                    }
                }
            }
        }
        return false;

    }

    public static boolean copy(File currFile) {

        if (htIgnore.containsKey(currFile.getAbsolutePath()) == false) {
            if (ht.containsKey(currFile.getAbsolutePath()) == true) {
                String lastDate = (String) ht.get(currFile.getAbsolutePath());
                if (Long.parseLong(lastDate) == currFile.lastModified()) {
                    put(currFile);
                    return false;
                }
            } else {
                put(currFile);
            }
        }
        return true;
    }

    private static void put(File currFile) {
        ht.remove(currFile.getAbsolutePath());
        ht.put(currFile.getAbsolutePath(), currFile.lastModified());
    }

    public static void getTimestamps(File file, Hashtable htCurrent) {


        BufferedReader input = null;
        try {

            input = new BufferedReader(new FileReader(file));
            String line = null;

            String name = "";
            String date = "";

            while ((line = input.readLine()) != null) {

                if (line.indexOf("|") > -1) {
                    name = line.substring(0, line.indexOf("|"));
                    date = line.substring(line.indexOf("|") + 1);
                } else {
                    name = line;
                    date = "";

                }
                htCurrent.put(name, date);

            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    private static void saveTimeStamps() {
        StringBuffer contents = new StringBuffer();
        Iterator it = ht.entrySet().iterator();
        for (int j = 0; j < ht.size(); j++) {

            Map.Entry entry = (Map.Entry) it.next();
            contents.append(entry.getKey());
            contents.append("|");
            contents.append(entry.getValue());
            contents.append("\n");
        }

        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(Constants.timeStamps));
            out.write(contents.toString());
            out.flush();
            out.close();
        } catch (IOException e) {
            System.out.print("error:" + e);
        }
    }

    public static void updateTimeStamps() {
        updateTimeStamps(new File(Constants.mavenrepo));
        saveTimeStamps();
    }

    private static void updateTimeStamps(File dir) {

        if (dir.isDirectory() == true) {
            for (File file : dir.listFiles()) {
                if (file != null) {
                    if (file.getName().contains(".jar") || file.getName().contains(".pom")) {
                        check(file);
                    }
                    updateTimeStamps(file);
                }
            }
        }
    }
}
/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 * All Rights Reserved.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
